﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using MyBetImporter.DAL;

namespace MyBetImporter
{
    [DataContractAttribute(IsReference = true)]
    [Serializable()]
    public class MatchObject
    {
        private int _matchId;
        private int _teamAwayId;
        private int _teamHomeId;
        private byte? _teamHomeScore;
        private byte? _teamAwayScore;
        private int _dayNumber;

        private byte? _teamHomeBetScore;
        private byte? _teamAwayBetScore;

        [Key()]
        [DataMemberAttribute()]
        public int MatchId
        {
            get { return _matchId; }
            set { _matchId = value; }
        }

        [DataMemberAttribute()]
        public int TeamAwayId
        {
            get { return _teamAwayId; }
            set { _teamAwayId = value; }
        }

        [DataMemberAttribute()]
        public int TeamHomeId
        {
            get { return _teamHomeId; }
            set { _teamHomeId = value; }
        }

        [DataMemberAttribute()]
        public byte? TeamHomeScore
        {
            get { return _teamHomeScore; }
            set { _teamHomeScore = value; }
        }

        [DataMemberAttribute()]
        public byte? TeamAwayScore
        {
            get { return _teamAwayScore; }
            set { _teamAwayScore = value; }
        }

        [DataMemberAttribute()]
        public int DayNumber
        {
            get { return _dayNumber; }
            set { _dayNumber = value; }
        }

        [DataMemberAttribute()]
        public byte? TeamHomeBetScore
        {
            get { return _teamHomeBetScore; }
            set { _teamHomeBetScore = value; }
        }

        [DataMemberAttribute()]
        public byte? TeamAwayBetScore
        {
            get { return _teamAwayBetScore; }
            set { _teamAwayBetScore = value; }
        }

        /// <summary>
        /// Create object from database information
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public static MatchObject CopyFrom(Matchs m)
        {
            MatchObject match = new MatchObject();
            match.MatchId = m.MatchId;
            match.TeamHomeId = m.Teams1.TeamId;
            match.TeamAwayId = m.Teams.TeamId;
            match.TeamHomeScore = m.ScoreHome;
            match.TeamAwayScore = m.ScoreAway;
            return match;
        }

        /// <summary>
        /// Add a new championship day in database
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="championshipId"></param>
        /// <param name="seasonId"></param>
        /// <param name="dayNumber"></param>
        /// <param name="openDate"></param>
        /// <param name="closeDate"></param>
        /// <summary>
        /// Add a new championship day in database
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="championshipId"></param>
        /// <param name="seasonId"></param>
        /// <param name="dayNumber"></param>
        /// <param name="openDate"></param>
        /// <param name="closeDate"></param>
        public static Matchs AddOrUpdate(ParieurProDBEntities dbContext, DateTime matchDate, int homeTeamId, int awayTeamId, byte? homeScore, byte? awayScore, int eventId)
        {
            ParieurProDBEntities db = dbContext;
            Matchs returnMatch = null;

            try
            {
                var query = (from match in db.Matchs
                             where match.TeamHomeId == homeTeamId && match.TeamAwayId == awayTeamId && match.EventId == eventId
                             select match);
                IList<Matchs> matchs = query.ToList();

                if (matchs.Count < 1)
                {
                    // Add
                    Matchs newMatch = new Matchs();
                    newMatch.Date = matchDate;
                    newMatch.TeamHomeId = homeTeamId;
                    newMatch.TeamAwayId = awayTeamId;
                    newMatch.ScoreHome = homeScore;
                    newMatch.ScoreAway = awayScore;
                    newMatch.EventId = eventId;
                    db.Matchs.AddObject(newMatch);
                    //dbContext.SaveChanges();
                    returnMatch = newMatch;
                }
                else
                {
                    // Update
                    matchs[0].Date = matchDate;
                    matchs[0].ScoreHome = homeScore;
                    matchs[0].ScoreAway = awayScore;
                    //dbContext.SaveChanges();
                    returnMatch = matchs[0];
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                return null;
            }

            return returnMatch;
        }

    }
}